package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.ChoiceOptions;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.TaskQuestions;
import org.example.commonapi.util.options.ChoiceOptionsService;
import org.example.course.service.TaskQuestionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
@Slf4j
public class TaskQuestionController {


    @Resource
    private TaskQuestionsService taskQuestionsService;

    @Resource
    private ChoiceOptionsService choiceOptionsService;

    /**
     * 获取随堂练习的所有题目
     * @param taskId
     * @return
     */
    @GetMapping("/list/task/questions/{taskId}")
    public R ListTaskQuestion(@PathVariable Long taskId){
        LambdaQueryWrapper<TaskQuestions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskQuestions::getTaskId,taskId);
        List<TaskQuestions> list=taskQuestionsService.list(wrapper);
        return R.success(list);
    }


    /**
     * 插入随堂练习的题目
     * @param taskQuestions
     * @return
     */
    @PostMapping("/insert/task/questions/{id}")
    public R insertTaskQuestion(@PathVariable Long id,@RequestBody List<TaskQuestions> taskQuestions) {
        for(TaskQuestions taskQuestion:taskQuestions){
            taskQuestion.setTaskId(id);
        }
        taskQuestionsService.saveBatch(taskQuestions);
        List<Long> ids=taskQuestions.stream().map(TaskQuestions::getId).collect(Collectors.toList());
        return R.success(ids);
    }

    /**
     * 插入随堂练习的选项
     * @param choiceOptions
     * @return
     */
    @PostMapping("/insert/task/question/options")
    public R insertTaskQuestionOptions(@RequestBody List<ChoiceOptions> choiceOptions){
        choiceOptionsService.saveBatch(choiceOptions);
        return R.success();
    }


    /**
     * 获取随堂练习的选项
     * @param id
     * @return
     */
    @GetMapping("/get/task/question/options/{id}")
    public R getTaskQuestionOptions(@PathVariable Long id){
        LambdaQueryWrapper<ChoiceOptions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChoiceOptions::getTaskQuestionId,id);
        ChoiceOptions choiceOptions=choiceOptionsService.getOne(wrapper);
        return R.success(choiceOptions);
    }



}
